﻿/*************************
 * 
 * by wzj 20130425
 * 
 * 中奖算法
 * 
 * 
 * 
 * 
 * */
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using Leefish.Market.PRO.Entity;
using JCW.Common;
using System.Threading;

namespace Leefish.Market.PRO.BLL
{
    
    public class BzWinBLL
    {
        private ArrayList arr;//中奖概率数组；
        private ArrayList arr2;//中奖
        private int bigArr1 = 0;//中奖率的大小空间
        private int bigArr2 = 0;//中奖的奖品概率值
        public int arrLen = 0;
        public BzWinBLL(EnRoller roller)
        {
            InitArr1(roller);
            InitArr2(roller);//初始化奖品中奖概率
        }

        /// <summary>
        /// 初始化中奖率
        /// </summary>
        /// <param name="roller"></param>
        private void InitArr1(EnRoller roller) {
            int len = 0;
            if (roller.Probability.ToString().Contains("."))
            {
                string[] arTemp = roller.Probability.ToString().Split('.');
                if (arTemp[1].Length == 1)
                {
                    //概率在千份之一
                    len = ConvertHelper.ToInt(roller.Probability) * 10;
                    len = 10;
                    arrLen = 1000;
                }
                else if (arTemp[1].Length >= 2)
                {
                    //概率最小值在万份之一
                    len = ConvertHelper.ToInt(roller.Probability) * 100;
                    len = 100;
                    arrLen = 10000;
                }
            }
            else
            {
                //概率在
                len = 1;
                if (roller.Probability.ToString().Length == 1)
                {
                    //最小概率在百分之一
                    arrLen = 100;
                }
                else if (roller.Probability.ToString().Length == 2)
                {
                    //最小概率在十分之一
                    arrLen = 100;
                }
                else
                {
                    //百分百中奖
                    arrLen = 100;
                }
            }
            bigArr1 = ConvertHelper.ToInt(ConvertHelper.ToDecimal(roller.Probability) * len);
            arr = new ArrayList(arrLen);
            for (int i = 0; i < arrLen; i++)
            {
                if (i < bigArr1) arr.Add(1);//初始化数组概率
                else arr.Add(0);

            }
        }

        /// <summary>
        /// 初始化中奖概率
        /// </summary>
        /// <param name="roller"></param>
        public void InitArr2(EnRoller roller) {
            //寻找最小值 
            int miniValue = 0;
            int len = 0;
            int tempLen;
            decimal chkValue = 0;
            foreach (EnAward aw in roller.ListAward) {
                if (aw.Probability.ToString().Contains("."))
                {
                    tempLen = aw.Probability.ToString().Split('.')[1].Length;
                    if (tempLen > len)
                        len = tempLen;
                }

                chkValue += ConvertHelper.ToDecimal(aw.Probability);
            }

            if (chkValue > 100) { 
                //失败

            }
            //miniValue = len;
            if (len == 0)
            {
                bigArr2 = 100;
                miniValue=1;
                //表示最小概率在1% 
            }
            else if (len == 1) {
                bigArr2 = 1000;
                miniValue=10;
            }
            else if (len == 2)
            {
                bigArr2 = 10000;
                miniValue=100;
            }
            else { 
                //初始化失败
            }
            arr2 = new ArrayList(bigArr2);//产生第二个数组
            int tempInt = 0;
            int bg=0;

            foreach (EnAward aw in roller.ListAward) {
                int scale = ConvertHelper.ToInt(ConvertHelper.ToDecimal(aw.Probability) * miniValue);
                for (int i = 0; i < scale; i++) {
                    //arr2[i + bg] = aw.AID;
                    arr2.Add(aw.AID);
                }
                bg += scale;
            }


        }

        public int Surprise()
        {
            Thread.Sleep(10);
            int rel = 0;
            long tick = DateTime.Now.Ticks;
            Random rad = new Random((int)(tick & 0xffffffffL) | (int)(tick >> 32));
            //Random rad = new Random();
            
            int r = rad.Next(0, arr.Count - 1);
            Console.Write(r);
            rel = ConvertHelper.ToInt(arr[r]);
            if (rel > 0) { 
            //有中奖
                Thread.Sleep(10);
                tick = DateTime.Now.Ticks;
                Random rad2 = new Random((int)(tick & 0xffffffffL) | (int)(tick >> 32));
                r = rad2.Next(0, arr2.Count - 1);
                Console.Write(r);
                rel = ConvertHelper.ToInt(arr2[r]);
            }

            return rel;
        }

        public void InitWin()
        { 
            
        }

        
    }
}
